package com.sc.nft.config.hf;

import com.alibaba.fastjson.JSONObject;
import com.qiniu.util.IOUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.security.KeyStore;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.util.Enumeration;

@Configuration
@Slf4j
public class HFInit {
    @Value("${pay.hfConfig.apiUrl:}")
    private String apiUrl;
    @Value("${pay.hfConfig.merCustId:}")
    private String merCustId;
    @Value("${pay.hfConfig.notifyUrl:}")
    private String notifyUrl;
    @Value("${pay.hfConfig.mallNotifyUrl:}")
    private String mallNotifyUrl;
    @Value("${pay.hfConfig.mallRefundNotifyUrl:}")
    private String mallRefundNotifyUrl;
    @Value("${pay.hfConfig.accountNotifyUrl}")
    private String accountNotifyUrl;
    @Value("${pay.hfConfig.merNo:}")
    private String merNo;
    @Value("${pay.hfConfig.acctId:}")
    private String acctId;
    @Value("${pay.hfConfig.pfx.name:}")
    private String privatePath;
    @Value("${pay.hfConfig.pub.name:}")
    private String publicKeyPath;
    @Value("${pay.hfConfig.pfx.pwd:}")
    private String privatePwd;
    @PostConstruct
    public void initHFConfig() throws Exception {
        HfConfig.apiUrl = apiUrl;
        HfConfig.notifyUrl = notifyUrl;
        HfConfig.mallNotifyUrl = mallNotifyUrl;
        HfConfig.accountNotifyUrl = accountNotifyUrl;
        HfConfig.merCustId = merCustId;
        HfConfig.version = "10";
        HfConfig.merId = merNo;
        HfConfig.acctId = acctId;
        HfConfig.privatePath = getKeyPath(privatePath);
        HfConfig.publicPath = getKeyPath(publicKeyPath);
        HfConfig.privateKey = IOUtils.toByteArray(getInputStream(privatePath));
        HfConfig.publicKey = IOUtils.toByteArray(getInputStream(publicKeyPath));
        HfConfig.privatePwd = privatePwd;
        HfConfig.mallRefundNotifyUrl = mallRefundNotifyUrl;
    }

    private InputStream getInputStream(String path) throws Exception {
        InputStream inputStream  = HFInit.class.getClassLoader()
                .getResourceAsStream(path);
        return inputStream;
    }


    private String getKeyPath(String publicKeyPath){
        return HFInit.class.getClassLoader().getResource(publicKeyPath).getPath();
    }

    /**
     * 获取私钥对象
     *
     * @param inputStream
     *            密钥算法
     * @return 私钥对象
     * @throws Exception
     */
    public static PrivateKey getPrivateKey(InputStream inputStream, String password) throws Exception {
        try {
            KeyStore ks = KeyStore.getInstance("PKCS12");
            char[] nPassword = null;
            if ((password == null) || password.trim().equals("")) {
                nPassword = null;
            } else {
                nPassword = password.toCharArray();
            }

            ks.load(inputStream, nPassword);
            Enumeration<String> enumas = ks.aliases();
            String keyAlias = null;
            if (enumas.hasMoreElements()) {
                keyAlias = (String) enumas.nextElement();
            }
            PrivateKey privateKey = (PrivateKey) ks.getKey(keyAlias, nPassword);
            return privateKey;
        } catch (FileNotFoundException e) {
            throw new Exception("私钥路径文件不存在");
        } catch (IOException e) {
            throw new Exception(e);
        } catch (NoSuchAlgorithmException e) {
            throw new Exception("生成私钥对象异常");
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
            }
        }
    }
}
